package com.gasolinator.a.util;

import android.app.*;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;
import com.gasolinator.a.GasolinatorActivity;
import com.gasolinator.a.R;
import com.gasolinator.a.app.helper.GasolinatorPreferences;
import com.gasolinator.a.util.persistence.DeviceIdGenerator;

import java.util.concurrent.TimeUnit;

public class GasolinatorUtil {

    private static ProgressDialog dialog;
    private static AlertDialog alertDialog = null;

    public static String getUniqueDeviceId(Context baseContext) {
        DeviceIdGenerator generator = new DeviceIdGenerator(baseContext);
        return generator.generate();
    }

    public static boolean isNetworkAvailable(Context baseContext) {
        ConnectivityManager connectionManager = (ConnectivityManager) baseContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean networkAvailable = connectionManager.getActiveNetworkInfo() != null && connectionManager.getActiveNetworkInfo().isConnected();
        return networkAvailable;

    }


    public static void showWaitingGasolinatorMessage(Context context) {
        if (dialog != null) {
            hideWaitingGasolinatorMessage();
        }

        String message = context.getString(R.string.loading);

        dialog = new ProgressDialog(context);
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setMessage(message);
        dialog.show();


        Log.d("progress", "Mostrando dialogo:" + message);
    }

    public static void hideWaitingGasolinatorMessage() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
            Log.d("progress", "Ocultando dialogo");
        }
    }

    public static void doDebug(String word, String contentTitle, Integer numSources, Context context, long trackerIniTime) {

        GasolinatorPreferences pref = new GasolinatorPreferences(context.getSharedPreferences("Dixio", Context.MODE_PRIVATE));
        if (!pref.isNotificationHidden()) {
            long when = System.currentTimeMillis();         // notification time

            Long timeToResponse = Math.abs(trackerIniTime - when);
            //TimeUnit.MILLISECONDS.toMinutes(timeToResponse),
            String secs = String.format("%d", TimeUnit.MILLISECONDS.toSeconds(timeToResponse));

            int icon = R.drawable.logo_gaswalker;        // icon from resources
            CharSequence tickerText = "Best Dixio answer...? Took " + secs + " secs.";              // ticker-text
            CharSequence contentText = "\"" + word + "\" found in " + secs + " secs at " + numSources + " sources";      // expanded message text

            Intent notificationIntent = new Intent(context, GasolinatorActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

            Notification notification = new Notification(icon, tickerText, when);
            //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
            notification.contentIntent = contentIntent;

            //notification.FLAG_AUTO_CANCEL;

            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, notification);
        }

//        final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
//        contentView.setImageViewResource(R.id.image, R.drawable.icon);
//        contentView.setTextViewText(R.id.text, tickerText);
//        contentView.setProgressBar(R.id.progress,100,0, false);
//        notif.contentView = contentView;


    }

    public static void showMessage(String title, String msg, Activity activity) {
        showMessage(title, msg, activity, false);
    }

    public static void showErrorMessage(String title, String msg, Activity activity) {
        showMessage(title, msg, activity, true);
    }

    public static void showMessage(String title, String msg, final Activity activity, final boolean finish) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (finish && activity != null) {
                    activity.finish();
                }
            }
        });
        alertDialog = builder.create();
        alertDialog.show();
    }

    public static void showGPSDisabledAlertToUser(final Activity activity){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
                .setCancelable(false)
                .setPositiveButton("Goto Settings Page To Enable GPS",
                        new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int id){
                                Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                activity.startActivity(callGPSSettingIntent);
                            }
                        });
        alertDialogBuilder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int id){
                        dialog.cancel();
                    }
                });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }

}




